//
//  UserDataManager.cpp
//  firebkball
//
//  Created by ren jian on 18/5/28.
//
//

#include "UserDataManager.h"

static UserDataManager *userManager = nullptr;

UserDataManager *UserDataManager::getInstance()
{
    if (userManager==nullptr) {
        userManager = new UserDataManager();
    }
    return userManager;
}

UserDataManager::UserDataManager()
{
    userData = new User();
    battleData = new BattleData();
}

UserDataManager::~UserDataManager()
{
    
}

void UserDataManager::addCoin(int _number)
{
    userData->coin+=_number;
}

//--------  战场数据 -------
void BattleData::calculateTheScore(bool _isKongxin, bool _isGaoshe, bool _isLanban)
{
    int l_score = 1;
    if (_isKongxin) {
        l_score = score_kongxin;
        if (_isLanban) {
            l_score += 2;
        }
        if (_isGaoshe) {
            l_score += 2;
        }
        l_score += hollowBallNumber;
        hollowBallNumber++;
    }else if (_isLanban){
        l_score = score_lanban;
        if (_isGaoshe) {
            l_score += 1;
        }
        hollowBallNumber = 0;
    }else if (_isGaoshe){
        l_score = score_gaoshe;
        
        hollowBallNumber = 0;
    }
    this->score += l_score;
}



//--------  用户数据 -------
int User::getTodayCoin()
{
    return DataStorageTool::getIntValue(StringUtils::format("coin_%s", MyFunction::getToday().c_str()));
}

void User::setTodayCoin()
{
    int l_todayCoin = this->getTodayCoin();
    DataStorageTool::saveIntValue(StringUtils::format("coin_%s", MyFunction::getToday().c_str()), l_todayCoin++);
}

void User::refreshScoreTop(int _newScore)
{
    for(int i=0; i<5; i++)
    {
        if (_newScore<=scoreTop[i]){
            continue;
        }
        int temp = scoreTop[i];
        scoreTop[i] = _newScore;
        _newScore = temp;
    }
    for(int i=0; i<5; i++)
    {
        CCLOG("---->>> %d", scoreTop[i]);
        DataStorageTool::saveIntValue(StringUtils::format("%s%d", MyConst::KEY_USER_TOP.c_str(), i+1), scoreTop[i]);
    }
}


